package com.hcy.rabbitruntimeapi.center.handler;

import com.alibaba.fastjson.JSON;
import com.hcy.rabbitbasicutils.utils.RedisUtils;
import com.hcy.rabbitruntimeapi.center.MessageHandler;
import com.hcy.rabbitruntimeapi.center.MyMessageHandler;
import com.hcy.rabbitbasicmodel.enums.M_Code;
import com.hcy.rabbitbasicmodel.common.MyWebSocketMessage;
import com.hcy.rabbitruntimeapi.model.message.driver.UploadTrackPointsDTO;
import com.hcy.schedulingclient.client.SchedulingLocationClient;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @Author huchenying
 * @Description
 * @Date 2021/12/15 19:28
 */
@MyMessageHandler(M_Code.DriverUploadTrack)
@Component
public class DriverUploadTrackHandler extends MessageHandler {

    Logger logger = LoggerFactory.getLogger(DriverUploadTrackHandler.class);

    @Autowired
    SchedulingLocationClient schedulingLocationClient;
    @Autowired
    RedisUtils redisUtils;

    @Override
    public void handler(MyWebSocketMessage myWebSocketMessage) {
        logger.debug("DriverUploadTrackHandler start code -> [{}] , desc -> [{}]", myWebSocketMessage.getCode(), myWebSocketMessage.getDesc());
        try {
            //解析数据体
            Object data = myWebSocketMessage.getData();
            String s = JSON.toJSONString(data);
            UploadTrackPointsDTO uploadTrackPointsDTO = JSON.parseObject(s, UploadTrackPointsDTO.class);
            //验证参数 ， 调用运力系统
            logger.info("解析结果 -> {}", ToStringBuilder.reflectionToString(uploadTrackPointsDTO));
            /**
             * @Author huchenying
             * @Description 上传位置中心的逻辑改到这里，不用多一次调用
             * @Date 2022/3/20 17:42
             **/
            //添加坐标
            redisUtils.addGeo("CAR_GEO_KEY",uploadTrackPointsDTO.getTid().toString(),uploadTrackPointsDTO.getLat(),uploadTrackPointsDTO.getLog());
            //添加key的有效时间
            long second = LocalDateTime.now().plusSeconds(30).toEpochSecond(ZoneOffset.of("+8"));
            redisUtils.zAdd("CAR_GEO_KEY_EXPIRE",uploadTrackPointsDTO.getTid().toString(),second);

//            ResponseData<Object> upload = schedulingLocationClient.upload(uploadLocationRequestDTO);
//            if(upload.getCode() != Code.Success){
//                logger.info("定位上传错误！");
//            }
        } catch (Exception e) {
            logger.error("处理失败！reason -> {}", ToStringBuilder.reflectionToString(e));
        }
    }
}
